package com.ymatou.iapi.trading.useranalysis.testcase;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.useranalysis.parameter.BuyerFlagQueryReq;
import com.ymatou.iapi.trading.useranalysis.service.BuyerFlagQueryCall;
import com.ymt.base.YmatouEasyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.OrderCallService;

import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.mongo.DeviceidWapper;
import com.ymttest.utils.YMTDateUtil;

public class Ts_BuyerFlagQuery {

	private static BuyerFlagQueryReq req;
	private static BuyerFlagQueryCall call;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询用户标签信息集合");
	}

	@Before
	public void caseUp() {
		req = new BuyerFlagQueryReq();
		call = new BuyerFlagQueryCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BuyerFlagQuery_001() {
		Logger.start(true, "查询标签为vip的用户");
		try {

			//disconf固定配置为92
			req.setBuyerId(String.valueOf(92));

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(Long.parseLong(req.getBuyerId()), call.getBuyerId(), "验证buyerId");
			Logger.verifyEquals(2L, call.getFlags().optLong(0), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BuyerFlagQuery_002() {
		Logger.start(true, "查询标签为新客的用户");
		try {
			
			Logger.comment("数据准备开始");
			OrderCallService orderCallService = new OrderCallService();

			int userId = orderCallService.createNewUser();
			TradingServiceResp resp = TradingPlaceOrderCallService.placeOrderWithUserId(userId);
			Logger.comment("数据准备结束");
			
			TradingSyncService.getSyncResult(resp.getOrderList().get(0).getOrderId());

			req.setBuyerId(String.valueOf(userId));

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(Long.parseLong(req.getBuyerId()), call.getBuyerId(), "验证buyerId");
			Logger.verifyEquals(1L, call.getFlags().optLong(0), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BuyerFlagQuery_003() {
		Logger.start(true, "buyerId为null");
		try {
			
			req.setBuyerId(null);
			
			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(false, call.getSuccess(), "验证success");
			Logger.verifyEquals("ILLEGAL_ARGUMENT", call.getErrorCode(), "验证errorCode");
			Logger.verifyEquals("buyerId:buyerId未提供", call.getErrorMessage(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BuyerFlagQuery_004() {
		Logger.start(true, "查询标签为非vip非新客的用户");
		try {
			
			req.setBuyerId(String.valueOf(93));

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(Long.parseLong(req.getBuyerId()), call.getBuyerId(), "验证buyerId");
			Logger.verifyEquals(0, call.getFlags().length(), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BuyerFlagQuery_005() {
		Logger.start(true, "激活时间大于用户注册的时间（当天）90天，查询标签为非新客的用户");
		try {
			
			Logger.comment("数据准备开始");
			OrderCallService orderCallService = new OrderCallService();

			int userId = orderCallService.createNewUser();
			TradingServiceResp resp = TradingPlaceOrderCallService.placeOrderWithUserId(userId);
			Logger.comment("数据准备结束");
			
			TradingSyncService.getSyncResult(resp.getOrderList().get(0).getOrderId());
			
			String deviceId = null;
			YmatouEasyCall getDeviceIdCall = new YmatouEasyCall("", "GET", "JSON");

			getDeviceIdCall
					.setUrl("http://deviceid.iapi.ymatou.com/getList?type=1&limit=1&userid="
							+ userId);

			try {
				getDeviceIdCall.callService();

				JSONObject data = new JSONObject(getDeviceIdCall.getReturnData())
						.optJSONArray("data").getJSONObject(0);

				if (null == data)
					return;
				else {

					Logger.debug("returndata:" + data);

					deviceId = data.optString("deviceid");
				}

			} catch (Exception e) {

				e.printStackTrace();
			}
			
			String newDate = YMTDateUtil.addDays(-91, YMTDateUtil.YMDTHMSS_STRING);
			
			DeviceidWapper deviceidWapper = new DeviceidWapper();
			deviceidWapper.updateByDeviceId(deviceId, newDate + "Z");

			req.setBuyerId(String.valueOf(userId));

			call.setData(req);
			call.callService();
			
			Logger.verifyEquals(true, call.getSuccess(), "验证success");
			Logger.verifyEquals(Long.parseLong(req.getBuyerId()), call.getBuyerId(), "验证buyerId");
			Logger.verifyEquals(0L, call.getFlags().optLong(0), "验证flag");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
